package com.mostromas.ess.product.controller;

import com.mostromas.ess.entities.Fabric;
import com.mostromas.ess.product.req.FabricREQ;
import com.mostromas.ess.product.service.IFabricService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 面料表-前端控制器
 */
@Api(value = "面料管理接口",description = "面料管理接口，提供面料的增、删、改、查")
@RestController //所有方法都会返回json字符串进行相应
@RequestMapping("/fabric")
public class FabricController {

    @Autowired
    private IFabricService fabricService;

    @PreAuthorize(("hasAuthority('fabric:search')"))
    @ApiOperation("根据面料名称查询面料成表接口")
    @PostMapping("/search")
    public Result search(@RequestBody FabricREQ req){
        return fabricService.queryPage(req);
    }

    @PreAuthorize(("hasAuthority('fabric:search')"))
    @ApiOperation("查询面料详情接口")
    @ApiImplicitParam(name = "id",value = "面料ID",required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return Result.ok(fabricService.findDetailsById(id));
    }

    @PreAuthorize(("hasAuthority('fabric:edit')"))
    @ApiOperation("修改面料信息接口")
    @PutMapping
    public Result update(@RequestBody Fabric fabric){
        fabricService.updateDetails(fabric,fabric.getCompositionList());
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('fabric:add')"))
    @ApiOperation("新增面料信息接口")
    @PostMapping
    public Result save(@RequestBody Fabric fabric){
        fabricService.saveFabricComposition(fabric,fabric.getCompositionList());
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('fabric:delete')"))
    @ApiOperation("删除面料信息接口")
    @ApiImplicitParam(name = "id",value = "面料ID",required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        fabricService.removeById(id);
        return Result.ok();
    }

}
